package com.odm.communitymanage.ui.mine.MyParkingPlace

import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.SPUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.odm.communitymanage.R
import com.odm.communitymanage.base.BaseMVVMFragment
import com.odm.communitymanage.core.Constants
import kotlinx.android.synthetic.main.fragment_parking_place_mine.*

/**
 * @description: 我的车位 页面 (带演示Example)
 * @author: ODM
 * @date: 2020/4/15
 */
class MyParkingPlaceFragment : BaseMVVMFragment(){

    lateinit var rvAdapter : ParkingPlaceAdapter
    var parkingList : MutableList<ParkingPlaceInfo> = mutableListOf()


    init {
        parkingList.add(ParkingPlaceInfo(0,"粤A12345",SPUtils.getInstance(Constants.SP_USER).getString(Constants.USER_COMMUNITY_NAME)+" B区A1101","12小时34分钟","2020-04-06  08:23 "))
        parkingList.add(ParkingPlaceInfo(1,"",SPUtils.getInstance(Constants.SP_USER).getString(Constants.USER_COMMUNITY_NAME)+" B区A1105","",""))
    }


    override fun initViewModelObserve() {

    }

    override fun initViews() {
        ivBack_ParkingPlace.setOnClickListener {
            pop()
        }
        btnBookParkingPlace.setOnClickListener {
            start(PostBookingParkingFragment())
        }
        //初始化车位列表
        rvMyParkingPlace.layoutManager = LinearLayoutManager(requireContext())
        rvAdapter = ParkingPlaceAdapter(parkingList)
        rvMyParkingPlace.adapter = rvAdapter


    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_parking_place_mine
    }


    data class ParkingPlaceInfo(
        val status : Int,
        val license_plate : String,
        val location : String ,
        val duration : String,
        val start_time : String
    )


    class ParkingPlaceAdapter (data : MutableList<ParkingPlaceInfo>)
        : BaseQuickAdapter<ParkingPlaceInfo, BaseViewHolder>(R.layout.item_parking_place_detail , data) {

        override fun convert(holder: BaseViewHolder, item: ParkingPlaceInfo) {
            var location = item.location
            if(location.length > 15){
                location = location.substring(0,15) + "..."
            }
            if(item.status == 0){
                holder.setText(R.id.tvState_PlacingPlace , "正在停车")
                holder.setTextColor(R.id.tvState_PlacingPlace , ColorUtils.string2Int("#FF5D5D"))
                holder.setText(R.id.tvLocation_ParkingPlace , location)
                holder.setText(R.id.tvCarNumber_ParkingPlace ,item.license_plate)
                holder.setText(R.id.tvDuration_ParkingPlace , item.duration)
                holder.setText(R.id.tvStartTime_ParkingPlace , item.start_time)
            }else {
                holder.setText(R.id.tvState_PlacingPlace , "空闲")
                holder.setTextColor(R.id.tvState_PlacingPlace , ColorUtils.getColor(R.color.colorAccent))
                holder.setText(R.id.tvLocation_ParkingPlace , location)
                holder.setText(R.id.tvCarNumber_ParkingPlace ,"暂无")
                holder.setText(R.id.tvDuration_ParkingPlace ,"暂无")
                holder.setText(R.id.tvStartTime_ParkingPlace , "暂无")
            }
        }
    }


}